/**
 * @file monitor_so.c
 * @brief Monitor.so file
 * @author Hongbao Chen
 * @version 1.0 Beta
 * @date 2012-04-20
 */

#ifndef SXML_H_
#include "sxml.h"
#endif
#ifndef MACHINFO_H_
#include "machinfo.h"
#endif
#ifndef SUTIL_H_
#include "sutil.h"
#endif
#ifndef MONITOR_H_
#include "monitor.h"
#endif

#include <ctype.h>
#include <signal.h>
#include <time.h>


#ifdef __cplusplus
extern "C" {
#endif

void exit_handler(void) {

}

#define SO_CFG_INFO_ITSEC		"so.info.interval_sec"
#define SO_CFG_INFO_ITNSEC		"so.info.interval_nsec"

#define DEF_SO_INFO_SEC			((int)60)
#define DEF_SO_INFO_NSEC		((int)0)

void info_xml(sigval_t _sigval) {
	printf("[TODO]Process machinfo.\n");
}


void setup_handler(Env *_pEnv) {

	if(_pEnv == NULL) {
		return;
	}
	
	struct itimerspec tval;
	struct sigevent se;
	timer_t timerid;

	char *itsec	= api_queryIni(&(_pEnv->ini_file), SO_CFG_INFO_ITSEC, NULL, 0);
	char *itnsec	= api_queryIni(&(_pEnv->ini_file), SO_CFG_INFO_ITNSEC, NULL, 0);

	int sec		= itsec==NULL?DEF_SO_INFO_SEC:atoi(itsec);
	int nsec	= itnsec==NULL?DEF_SO_INFO_NSEC:atoi(itnsec);

	se.sigev_notify	= SIGEV_THREAD;
	se.sigev_notify_function = info_xml;
	se.sigev_value.sival_ptr = _pEnv;

	if(timer_create(CLOCK_REALTIME, &se, &timerid) == -1) {
		API_ERR_LOG("[WARNING]timer_create() fail.");
		return;
	}

	tval.it_value.tv_sec		= sec;
	tval.it_value.tv_nsec		= nsec;
	tval.it_interval.tv_sec		= sec;
	tval.it_interval.tv_nsec	= nsec;
	
	if(timer_settime(timerid, 0, &tval, NULL) == -1) {
		API_ERR_LOG("[WARNING]timer_settime() fail.");
		return;
	}

	char buf[MAX_BUF_SIZE];
	snprintf(buf, MAX_BUF_SIZE-1, "[INFO]Timer info: %d sec, %d nano sec.", sec, nsec);
	API_ERR_LOG(buf);

	return;
}


void in_handler(Env *_pEnv, int _sockFd, struct sockaddr_in _sock) {

	String ins;
	bzero(&ins, sizeof(String));
	int n= readFd(_sockFd, &ins);
	if(n <= 0) {
		return;
	}
	API_ERR_LOG(ins.phead);
	return;

}

#ifdef __cplusplus
}
#endif

